<extend name="Public:base" />
<block name="content">
<div class="row">
    <div class="col-xs-12">
        <form class="form-horizontal"  id="validation-form" enctype="multipart/form-data"  method="post" onsubmit="return check()" action='{:U("category/addtwo")}'>
            <div class="form-group required">
                <label class="col-sm-2 control-label">二级分类名称：</label>
                <div class="col-sm-10">
                    <input  class="form-control" style="width:220px;" type="text" placeholder="" name="category_name"  value="" />
                </div>
            </div>
            <div class="form-group required">
                <label class="col-sm-2 control-label">服务需知：</label>
                <div class="col-sm-10">
                    <textarea  class="form-control" style="width:600px;height: 385px"  name="desc" ></textarea>

                </div>
            </div>
            <div class="form-group required">
                <label class="col-sm-2 control-label">排序：</label>
                <div class="col-sm-10">
                    <input  class="form-control" style="width:220px;" type="text" placeholder="" name="order" value=""/>
                </div>
            </div>
            <div class="form-group required">
                <label class="col-sm-2 control-label">标准诊断费：</label>
                <div class="col-sm-10">
                    <input  class="form-control" style="width:220px;" type="text" placeholder="" name="standard" value=""/>
                </div>
            </div>
            <div class="form-group required">
                <label class="col-sm-2 control-label">专修诊断费：</label>
                <div class="col-sm-10">
                    <input  class="form-control" style="width:220px;" type="text" placeholder="" name="expert" value=""/>
                </div>
            </div>
            <div class="form-group required">
                <label class="col-sm-2 control-label">顺风诊断费：</label>
                <div class="col-sm-10">
                    <input  class="form-control" style="width:220px;" type="text" placeholder="" name="wind" value=""/>
                </div>
            </div>
            <div class="form-group required">
                <label class="col-sm-2 control-label">分类：</label>
                <div class="col-sm-10">
                    <select name="type">
                        <option>请选择类型</option>
                        <volist name="select" id="v">
                            <option value="{$v.category_id},{$v.category_name}">{$v.category_name}</option>
                        </volist>
                    </select>
                </div>
            </div>
            <div class="form-group required">
                <label class="col-sm-2 control-label">状态：</label>
                <label><input type="radio"  name="status" value="1" onclick="" checked="checked"/>开启</label>&nbsp;&nbsp;&nbsp;&nbsp;
                <label><input type="radio" name="status" value="2" onclick="" />关闭</label>
            </div>

            <div class="form-group required">
                <label class="col-sm-2 control-label">上传图片：</label>
                <div class="col-sm-10" id="up">
                    <input type="file" name="icon" id="icon" onchange="preview(event)">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label no-padding-left" style="visibility: hidden;">提交：</label>
                <div class="col-sm-10">
                    <input  type="submit" value="提交" class="btn btn-primary btn-sm radius5" />
                    <a class="btn btn-primary btn-sm radius5" href="{:U('Category/two')}"> 返回 </a>
                </div>
            </div>
        </form>
    </div>
</div>
</block>
<block name="javascript">
    <script>
        function check(){
            var name = $('input[name=category_name]').val();
            var desc = $('input[name=desc]').val();
            var order =$('input[name=order]').val();
            var standard =$('input[name=standard]').val();
            var expert =$('input[name=expert]').val();
            var wind =$('input[name=wind]').val();
            var select = $("select").val();

            var img = document.getElementById("img");

            if($.trim(name)==''){
                $.jbox.info('分类名称不能为空！','提示');
                return false;
            }
            if($.trim(order)==''){
                $.jbox.info('排序不能为空！','提示');
                return false;
            }
            if(isNaN($.trim(order))){
                $.jbox.info('排序请输入数字！','提示');
                return false;
            }
            if($.trim(standard)==''){
                $.jbox.info('标准诊断费不能为空！','提示');
                return false;
            }
            if($.trim(expert)==''){
                $.jbox.info('专修诊断费不能为空！','提示');
                return false;
            }
            if($.trim(wind)==''){
                $.jbox.info('顺丰诊断费不能为空！','提示');
                return false;
            }
            if(isNaN(standard)||isNaN(expert)||isNaN(wind)){
                $.jbox.info('诊断费请输入数字！','提示');
                return false;
            }
            if(select=='请选择类型'){
                $.jbox.info('请选择一个分类！','提示');
                return false;
            }
            if(!img){
                $.jbox.info('请加入图片！','提示')
                return false;
            }

            $('#validation-form').ajaxSubmit(function(data){
                if(data=='1'){
                    $.jbox.tip("添加成功！", 'loading');
                    window.setTimeout(function () {window.location.href="{:U('Category/two')}"; }, 500);
                }if(data=='2'){
                    $.jbox.info('添加失败！','提示');
                }if(data=='-1'){
                    $.jbox.info('图片添加失败！','提示');
                }
            });

            return false;

        }

        function preview(event) {
            var files = event.target.files;
            var reader  = new FileReader();

            reader.onload = function (event) {
                var result = event.target.result;

                var x = document.getElementById("icon");
                if (!x || !x.value) return;
                var patn = /\.jpg$|\.jpeg$|\.gif$|\.png$/i;
                if (patn.test(x.value)) {
                    var y = document.getElementById("img");
                    if (y) {
                        y.src = result;
                    } else {
                        var img = document.createElement("img");
                        img.setAttribute("src", result);
                        img.setAttribute("width", "145");
                        img.setAttribute("height", "155");
                        img.setAttribute("id", "img");
                        document.getElementById("up").appendChild(img);
                    }
                } else {
                    $.jbox.info("您选择的似乎不是图像文件!",'提示');
                }


                reader = null;
            }
            reader.readAsDataURL(files[0]);

        }


    </script>
</block>


